package com.aier.user.entity.userapp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

public class AdvertRowMapper implements RowMapper<Advert>{
    private int type;
//    public static final int AND_DATESTR = 1;

    public AdvertRowMapper() {
    }

    public AdvertRowMapper(int type) {
        this.type = type;
    }

    @Override
    public Advert mapRow(ResultSet rs, int rowNum) throws SQLException {
        Advert um = null;
//        switch (type) {
//            case AND_DATESTR:
//                um = parseAdvert(new Advert(), rs, "um");
//                um.setSdate_str(SystemDateFormatUtil.formatDate(um.getSdate(), SystemDateFormatUtil.FORMAT_DATE));
//                um.setEdate_str(SystemDateFormatUtil.formatDate(um.getEdate(), SystemDateFormatUtil.FORMAT_DATE));
//                break;
//            default:
//                um = parseAdvert(new Advert(), rs);
//        }
        um = parseAdvert(new Advert(), rs);
        return um;
    }

    public static Advert parseAdvert(Advert s, ResultSet rs) throws SQLException {
        return parseAdvert(s, rs, null);
    }

    public static Advert parseAdvert(Advert s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setAid(rs.getLong(su+"aid"));
        } catch (SQLException e) {
            s.setAid(null);
        }
        try {
            s.setName(rs.getString(su+"name"));
        } catch (SQLException e) {
            s.setName(null);
        }
        try {
            s.setDesinfo(rs.getString(su+"desinfo"));
        } catch (SQLException e) {
            s.setDesinfo(null);
        }
        try {
            s.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            s.setImg(null);
        }
        

        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, Advert s) {
        par.put("aid", s.getAid());
        par.put("name", s.getName());
        par.put("desinfo", s.getDesinfo());
        par.put("img",s.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, Advert s) {
        args.put("aid",s.getAid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final Advert s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "aid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(Advert s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into userapp_advert(");
        int count = 0;
        if (null != s.getAid()) {
            sql.append("aid,");
            count++;
        }
        if (null != s.getName()) {
            sql.append("name,");
            count++;
        }
        if (null != s.getDesinfo()) {
            sql.append("desinfo,");
            count++;
        }
        if (null !=s.getImg()) {
            sql.append("img,");
            count++;
        }
        
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, Advert s)
        throws SQLException {
        int count = 1;
        if (null != s.getAid()) {
            ps.setLong(count++,s.getAid());
        }
        if (null != s.getName()) {
            ps.setString(count++,s.getName());
        }
        if (null != s.getDesinfo()) {
            ps.setString(count++,s.getDesinfo());
        }
        if (null != s.getImg()) {
            ps.setString(count++,s.getImg());
        }
        

    }
}
